<template>
  <view class="">
    <view class="example">
                 		<uni-file-picker style="width: 80%;" class="addfile" limit="1" file-mediatype="all" file-extname="pdf"  @select="select" v-model="pdfValue"  ref="upfile"
    @fail="fail" ></uni-file-picker>
    <view style="margin-top: 40rpx;">提示：使用“网帮云笔”电脑端，效果更佳。</view>
  </view>
  </view>
</template>

<script>
  export default {
      
    data() {
      return {
        pdfValue:[],
      }
    },
    methods:{
      select(e){
      				console.log('选择文件：',e)
             if(e.tempFiles.length == 0) return
             let arr =e.tempFiles[0].file.name
             this.rename = arr.substring(0,arr.length-4)
             console.log(this.rename);
             
             this.userFile = e.tempFiles[0].file.path
             uni.uploadFile({
                 	url: getApp().globalData.apiDomain+'convert/capi', //仅为示例，非真实的接口地址
                   header: {
                       'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
                   	'token':uni.getStorageSync('wbyun_token'),
                     'wbapp':'weixin_mp_xbgy'
                   },
                 	filePath: this.userFile,
                 	name: 'file', 
                 	formData: {
                 		'o':'docx',
                 		'vpsoft':'c_pdf2docx'
                 	},
                 	success: (uploadFileRes) => {
                 	   console.log(uploadFileRes) 
                 	   let data = JSON.parse(uploadFileRes.data)
                 	   console.log(data);
                 	   if(data.code == 200) {
                        uni.showLoading({
                          title:'转换中...',
                          mask:true
                        })
                 	     let cid = data.datas.cid
                 	     let timer = setInterval(() => {
                 	       uni.request({
                 	       	url: 'https://capi.wby360.com/api/getDownloadPath/cid/'+cid,
                 	       	method: 'GET',
                 	       	header: {
                 	       		'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
                 	       		'token': uni.getStorageSync(
                 	       			'wbyun_token'),
                               'wbapp':'weixin_mp_xbgy'
                 	       	},
                 	       	data: {},
                 	       	dataType: 'json',
                 	       	success: res => {
                 	       		console.log(res);
                 	          if(res.data.code == 200) {
                 	            let url = res.data.datas.url
                               console.log(url);
                 	            uni.hideLoading()
                 	            uni.showToast({
                 	              	title: '处理成功',
                 	              	icon: 'none'
                 	            });
                 	            clearInterval(timer)
                               uni.downloadFile({
                                 url: url, // 文件下载地址
                                 success: response => {
                                   if (response.statusCode === 200) {
                                     uni.getFileSystemManager().saveFile({
                                       tempFilePath: response.tempFilePath,
                                       success: (resData) => {
                                         uni.openDocument({
                                           filePath: resData.savedFilePath,
                                           showMenu: true, // 允许出现分享功能
                                           success: r => {
                                             console.log(r)
                                             this.clearbtn()
                                           },
                                           fail: openError => {
                                             console.log('打开失败: ', openError)
                                           }
                                         })
                                       },
                                       fail: error => {
                                         console.log('error: ', error)
                                       }
                                     })
                                   }
                                 }
                               })
            
                 	                        	 
                 	          }else if(res.data.code == 300) {
                 	            
                 	          }else {
                 	            uni.showToast({
                 	              	title: '文件处理失败请稍后再试!',
                 	              	icon: 'none'
                 	            });
                 	            clearInterval(timer)
                 	          }
                 	       
                 	       	},
                           fail: () => {},
                           complete: () => {
                           }
                 	       	
                 	       });
                 	     },1500)
                 	                        	 
                 	   }else {
                       uni.hideLoading()
                       uni.showToast({
                         	title: data.msg,
                         	icon: 'none'
                       });
                       this.clearbtn()
                       setTimeout(() => {
                         uni.navigateTo({
                           url:'/pagesB/subson/subson/apply'
                         })
                       },2000)
                     }
                 	}
                 });
      			},

      			// 上传失败
      			fail(e){
      				console.log('上传失败：',e)
      			},
            clearbtn() {
              this.$refs.upfile.clearFiles()
            }
    }
  }
</script>

<style>
  .example {
    text-align: center;
    margin-top: 100rpx;
  }
  button[type=primary] {
    width: 80%;
    height: 100rpx;
    line-height: 100rpx;
    font-size: 35rpx;
    background-color: #4193FE;
  }
 button[type=primary]:active {
   background-color: #4193FE !important;
 }
</style>